<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="隐患等级" name="hiddenLevel">
						<a-select v-model:value="searchFormState.hiddenLevel" placeholder="请选择隐患等级" :options="hiddenLevelOptions" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="任务名称" name="taskName">
						<a-input v-model:value="searchFormState.taskName" placeholder="请输入任务名称" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="执行状态" name="currentStatus">
						<a-select v-model:value="searchFormState.currentStatus" placeholder="请选择执行状态" :options="currentStatusOptions" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="领取日期" name="receiverDate">
						<a-range-picker v-model:value="searchFormState.receiverDate" value-format="YYYY-MM-DD"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			bordered
			:row-key="(record) => record.taskId"
			:tool-config="toolConfig"
		>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'hiddenLevel'">
					<a-tag color="cyan" v-if="record.hiddenLevel==null">暂未定级</a-tag>
					<a-tag color="green" v-if="record.hiddenLevel==0">一般问题</a-tag>
					<a-tag color="blue" v-if="record.hiddenLevel==1">一般隐患</a-tag>
					<a-tag color="orange" v-if="record.hiddenLevel==2">较大隐患</a-tag>
					<a-tag color="red" v-if="record.hiddenLevel==3">重大隐患</a-tag>
				</template>
				<template v-if="column.dataIndex === 'approval'">
					<a-tag color="blue" v-if="record.approval==0">待分配</a-tag>
					<a-tag color="orange" v-if="record.approval==1">分管领导审批</a-tag>
					<a-tag color="green" v-if="record.approval==2">审核完成</a-tag>
				</template>
				<template v-if="column.dataIndex === 'currentStatus'">
					<a-tag color="cyan" v-if="record.currentStatus==null">审核中</a-tag>
					<a-tag color="blue" v-if="record.currentStatus==0">已发布</a-tag>
					<a-tag color="blue" v-if="record.currentStatus==1">待执行</a-tag>
					<a-tag color="orange" v-if="record.currentStatus==2">执行中</a-tag>
					<a-tag color="green" v-if="record.currentStatus==3">执行完成</a-tag>
					<a-tag color="red" v-if="record.currentStatus==4">任务失败</a-tag>
				</template>
				<template v-if="column.dataIndex === 'payed'">
					<a-tag color="blue" v-if="record.payed==0">未支付</a-tag>
					<a-tag color="green" v-if="record.payed==1">已支付</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="viewFormRef.onOpen(record)">查看详情</a>
						<a-divider type="vertical" v-if="record.approval==1" />
						<a @click="formRef.onOpen(record)" v-if="record.approval==1">审批</a>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
	<ViewForm ref="viewFormRef" />
</template>

<script setup name="task">
	import Form from './form.vue'
	import ViewForm from './view.vue'
	import taskApi from '@/api/biz/task/taskApi'
	import tool from "@/utils/tool"
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const viewFormRef = ref()
	const hiddenLevelOptions = tool.dictList('hiddenLevel')
	const approvalOptions = ref([
		{'value':'0','label':'待分配'},
		{'value':'1','label':'分管领导审批'},
		{'value':'2','label':'审核完成'}])
	const currentStatusOptions = ref([
		{'value':'0','label':'已发布'},
		{'value':'1','label':'待执行'},
		{'value':'2','label':'执行中'},
		{'value':'3','label':'执行完成'},
		{'value':'4','label':'任务失败'}])
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }

	const columns = [
		{
			title: '任务名称',
			dataIndex: 'taskName',
			align:'center'
		},
		{
			title: '隐患等级',
			dataIndex: 'hiddenLevel',
			align:'center'
		},
		{
			title: '审批状态',
			dataIndex: 'approval',
			align:'center'
		},
		{
			title: '执行状态',
			dataIndex: 'currentStatus',
			align:'center'
		},
		{
			title: '盯防奖励',
			dataIndex: 'rewards',
			align:'center'
		},
		{
			title: '任务开始时间',
			dataIndex: 'receiverTime',
			align:'center'
		},
		{
			title: '任务结束时间',
			dataIndex: 'endTime',
			align:'center'
		},
		{
			title: '是否已经支付',
			dataIndex: 'payed',
			align:'center'
		},
		{
			title: '分管领导',
			dataIndex: 'chargeLeaderName',
			align:'center'
		},
		{
			title: '备注',
			dataIndex: 'taskRemark',
			align:'center'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '220px'
		},
	]

	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		if (searchFormParam.receiverDate) {
			searchFormParam.receiverDateStart = searchFormParam.receiverDate[0]
			searchFormParam.receiverDateEnd = searchFormParam.receiverDate[1]
			delete searchFormParam.receiverDate
		}
		searchFormParam.currentStatus = 2
		return taskApi.taskPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
</script>
